package cn.tqfeiyang.smartadmin.business.organization.validator;

import cn.tqfeiyang.smartadmin.business.organization.entity.AddressEntity;
import cn.tqfeiyang.smartadmin.business.organization.mapper.AddressMapper;
import cn.tqfeiyang.smartadmin.commons.mybatis.IEntityValidator;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import cn.hutool.core.lang.*;
import cn.tqfeiyang.smartadmin.commons.exception.*;
import cn.tqfeiyang.smartadmin.commons.toolbox.*;
import cn.tqfeiyang.smartadmin.business.organization.mapper.EmployeeMapper;

/**
 * 地址实体验证器
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Component
public class AddressValidator implements IEntityValidator<AddressEntity> {

    /**
     * 地址映射接口
     */
    @Resource
    private AddressMapper addressMapper;

    /**
     * 员工映射接口
     */
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 验证实体数据
     *
     * @param entity 地址实体
     * @param creating 是否是添加
     */
    @Override
    public void validate(AddressEntity entity, boolean creating) {
        if (NumberUtils.nonZero(entity.getEmployeeId())) {
            // 检查员工id是否存在？
            if (!employeeMapper.isExists(entity.getEmployeeId())) {
                throw new BusinessException("员工id不存在({})", entity.getEmployeeId());
            }
        }
    }

    /**
     * 删除前检查
     *
     * @param entity 地址实体
     */
    @Override
    public void beforeDelete(AddressEntity entity) {
    }

    /**
     * 删除后处理
     *
     * @param entity 地址实体
     */
    @Override
    public void afterDelete(AddressEntity entity) {
    }
}